package com.lysj.admin.master.bussiness.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lysj.admin.master.bussiness.admin.domain.CompanyProviderBind;
import com.lysj.admin.master.bussiness.admin.mapper.CompanyProviderBindMapper;
import com.lysj.admin.master.bussiness.admin.service.CompanyProviderBindService;
import com.lysj.admin.master.bussiness.admin.service.ServiceProviderService;
import com.lysj.admin.master.bussiness.company.company.dto.CompanyBaseDto;
import com.lysj.admin.master.common.CommonConstant;
import com.lysj.admin.master.common.annotation.MyList;
import com.lysj.admin.master.common.annotation.TokenInfo;
import com.lysj.admin.master.common.annotation.Update;
import com.lysj.admin.master.common.spring.mpbase.MyController;
import com.lysj.admin.master.common.spring.mpbase.MyService;
import com.lysj.admin.master.common.spring.pagination.PageRequestDTO;
import com.lysj.admin.master.common.web.ParamUtil;
import com.lysj.admin.master.common.web.Resp;
import com.lysj.admin.master.common.web.RespUtil;
import com.lysj.admin.master.common.web.SelectItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Created by drj on 2020-01-03 16:22
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/admin/company_provider_bind")
public class CompanyProviderBindController extends MyController<CompanyProviderBindMapper, CompanyProviderBind, PageRequestDTO> {

    @Resource
    private CompanyProviderBindService companyProviderBindService;
    @Resource
    private ServiceProviderService serviceProviderService;


    @Override
    public MyService<CompanyProviderBindMapper, CompanyProviderBind> getService() {
        return companyProviderBindService;
    }


    @Override
    @Update
    public Resp update(CompanyProviderBind entity) {
        return super.update(entity);
    }

    /*
     * @author Created by drj on 2020/1/3 16:28
     * @Description :获取用工平台未关联服务商selectItem
     */
    @GetMapping("/left/select_item")
    public Resp leftSelectItem(String companyId) {
        return RespUtil.success(companyProviderBindService.leftSelectItem(companyId));
    }

    /*
     * @author Created by drj on 2020/1/6 14:08
     * @Description :获取用工平台已关联服务商selectItem
     */
    @GetMapping("/relevance/select_item")
    public Resp relevanceSelectItem() {
        String companyId = getJwtProperty(CommonConstant.COMPANY_ID_NAME);
        List<String> byCompanyIdSelectItem = companyProviderBindService.findByCompanyIdSelectItem(companyId);
        List<SelectItem<String, String>> selectItemsSer = serviceProviderService.selectItemsByIds(byCompanyIdSelectItem);
        return RespUtil.success(selectItemsSer);
    }

    /*
     * @author Created by drj on 2020/1/6 14:26
     * @Description :用工平台与服务商进行绑定
     */
    @PostMapping("/add_relevance")
    public Resp add(String companyId, String[] strings) {
        companyProviderBindService.addRelevance(companyId, strings);
        return RespUtil.success("操作成功");
    }

    /*
     * @author Created by drj on 2020/1/6 15:47
     * @Description :余额明细
     */
    @Override
    @MyList
    public Resp list(PageRequestDTO condition) {
        return super.list(condition);
    }


    /*
     * @author Created by drj on 2020/1/6 15:47
     * @Description :充值账户列表
     */
    @GetMapping("/recharge_list")
    public Resp rechargeList(CompanyBaseDto companyBaseDto) {
        QueryWrapper<CompanyProviderBind> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id", ParamUtil.isBlank(companyBaseDto.getCompanyId()) ? getJwtProperty(CommonConstant.COMPANY_ID_NAME) : companyBaseDto.getCompanyId());
        wrapper.orderByDesc("p.create_time");
        Page<CompanyProviderBind> page = new Page<>(companyBaseDto.getPageNumber(), companyBaseDto.getPageSize());
        return RespUtil.success(companyProviderBindService.getBaseMapper().findByCompanyIdRechargeRecord(page, wrapper));
    }

    /*
     * @author Created by drj on 2020/1/7 17:08
     * @Description :余额明细
     */
    @GetMapping("/balance_list")
    public Resp balanceList(PageRequestDTO pageRequestDTO) {
        QueryWrapper<CompanyProviderBind> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id", getJwtProperty(CommonConstant.COMPANY_ID_NAME));
        wrapper.orderByDesc("p.create_time");
        Page<CompanyProviderBind> page = new Page<>(pageRequestDTO.getPageNumber(), pageRequestDTO.getPageSize());
        return RespUtil.success(companyProviderBindService.getBaseMapper().findByCompanyIdBalance(page, wrapper));
    }


    /*
     * @author Created by drj on 2020/1/13 16:59
     * @Description :获取当前用工企业绑定对应服务商资产(账户余额)
     */
    @GetMapping("/show_balance")
    public Resp showBalance(@TokenInfo(property = CommonConstant.COMPANY_ID_NAME) String companyId, String serviceProviderId) {
        CompanyProviderBind companyProviderBind = companyProviderBindService.findByCompanyIdAndServiceProviderId(companyId, serviceProviderId);
        return RespUtil.success(ParamUtil.isBlank(companyProviderBind.getBalance()) ? BigDecimal.ZERO : companyProviderBind.getBalance());
    }
}
